# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html


# useful for handling different item types with a single interface
from itemadapter import ItemAdapter
from settings import SQL_DB
import sqlite3

conn = sqlite3.connect(SQL_DB)
cursor = conn.cursor()


class AirspiderPipeline:
    def process_item(self, item, spider):
        sql = "INSERT INTO airData (date, pm25, aqi, pm10, so2, no2, o3, co, city, status)" \
              'VALUES ("{}", {}, {}, {}, {}, {}, {}, {}, "{}", 1)'
        sql = sql.format(item["date"], item["pm25"], item["aqi"], item["pm10"], item["so2"],
                         item["no2"], item["o3"], item["co"], item["city"])
        cursor.execute(sql)
        conn.commit()

        return item
